package com.pearl.boot.swagger;

import com.pearl.boot.profile.DevProfile;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * SpringDoc 自动配置
 *
 * @author TangDan
 * @version 1.0
 * @since 2022/9/5
 */
@DevProfile
@AutoConfiguration
@ConditionalOnProperty(
        prefix = "pearl.boot.swagger",
        name = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
)
public class SpringDocAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "pearl.boot.swagger")
    public SpringDocProperties springDocProperties() {
        return new SpringDocProperties();
    }

    @Bean
    public ConsoleDocAddressApplicationRunner consoleDocAddressApplicationRunner() {
        return new ConsoleDocAddressApplicationRunner();
    }

    @Bean
    public OpenAPI openAPI(SpringDocProperties springDocProperties) {
        return new OpenAPI()
                .info(new Info()
                        .title(springDocProperties.getTitle())
                        .description(springDocProperties.getDescription())
                        .version(springDocProperties().getVersion()));
    }

    @Bean
    public GroupedOpenApi groupedOpenApi(SpringDocProperties springDocProperties) {
        return GroupedOpenApi.builder().group(springDocProperties.getTitle()).pathsToMatch("/**").build();
    }
}
